from typing import Optional

from fastapi import APIRouter, Request
from fastapi import Depends
from sqlalchemy.orm import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeTerminalVO
from core.service.cms_gaode_terminal_service import CmsGaodeTerminalService
from utils.log_util import *
from utils.response_util import *

gaodeTerminalController = APIRouter()


@gaodeTerminalController.post("/api/gaode_terminal/query_by_terminal_id", response_model=CmsGaodeTerminalVO)
async def get_gaode_terminal_by_terminal_id(
    request: Request,
    terminal_id: str,
    start_time: Optional[str] = None,
    end_time: Optional[str] = None,
    query_db: Session = Depends(get_db)
):
    try:
        # 获取全量数据
        cms_gaode_terminal = CmsGaodeTerminalService.get_cms_gaode_terminal_by_terminal_id(
            query_db,
            terminal_id,
            start_time,
            end_time
        )
        logger.info('获取成功')
        return response_200(data=cms_gaode_terminal, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

# @gaodeTerminalController.post("/api/gaode_terminal/query_by_terminal_id", response_model=CmsGaodeTerminalVO)
# async def get_gaode_terminal_by_terminal_id(request: Request, terminal_id: str, query_db: Session = Depends(get_db)):
#     try:
#         # 获取全量数据
#         cms_gaode_terminal = CmsGaodeTerminalService.get_cms_gaode_terminal_by_terminal_id(query_db, terminal_id)
#         logger.info('获取成功')
#         return response_200(data=cms_gaode_terminal, message="获取成功")
#     except Exception as e:
#         logger.exception(e)
#         return response_500(data="", message=str(e))


@gaodeTerminalController.post("/gaode_terminal/query_like_terminal_name", response_model=CmsGaodeTerminalVO)
async def get_gaode_terminal_like_terminal_name(request: Request, terminal_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_terminal_list = CmsGaodeTerminalService.get_cms_gaode_terminal_like_name(query_db, terminal_name)
        logger.info('获取成功')
        return response_200(data=cms_gaode_terminal_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTerminalController.post("/gaode_terminal/add", response_model=CrudResponse)
async def add_gaode_terminal(request: Request, cms_gaode_terminal: CmsGaodeTerminalVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_terminal_result = await CmsGaodeTerminalService.add_cms_gaode_terminal(request, query_db, cms_gaode_terminal)
        if cms_gaode_terminal_result.is_success:
            logger.info(cms_gaode_terminal_result.message)
            return response_200(data=cms_gaode_terminal_result, message=cms_gaode_terminal_result.message)
        else:
            logger.warning(cms_gaode_terminal_result.message)
            return response_400(data="", message=cms_gaode_terminal_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTerminalController.patch("/gaode_terminal/edit", response_model=CrudResponse)
async def edit_gaode_terminal(request: Request, cms_gaode_terminal: CmsGaodeTerminalVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_terminal_result = await CmsGaodeTerminalService.edit_cms_gaode_terminal(query_db, cms_gaode_terminal)
        if cms_gaode_terminal_result.is_success:
            logger.info(cms_gaode_terminal_result.message)
            return response_200(data=cms_gaode_terminal_result, message=cms_gaode_terminal_result.message)
        else:
            logger.warning(cms_gaode_terminal_result.message)
            return response_400(data="", message=cms_gaode_terminal_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTerminalController.post("/gaode_terminal/delete", response_model=CrudResponse)
async def delete_gaode_terminal(request: Request, cms_gaode_terminal: CmsGaodeTerminalVO, query_db: Session = Depends(get_db)):
    try:
        delete_gaode_terminal_result = CmsGaodeTerminalService.delete_cms_gaode_terminal(query_db,
                                                                                         cms_gaode_terminal.id,
                                                                                         cms_gaode_terminal.service_id,
                                                                                         cms_gaode_terminal.terminal_id)
        if delete_gaode_terminal_result.is_success:
            logger.info(delete_gaode_terminal_result.message)
            return response_200(data=delete_gaode_terminal_result, message=delete_gaode_terminal_result.message)
        else:
            logger.warning(delete_gaode_terminal_result.message)
            return response_400(data="", message=delete_gaode_terminal_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
